public class SlidingWindow {
    public int longestOnes(int[] nums, int k) {
        int n = nums.length;
        int count = 0;
        int len = 0;
        for (int left = 0, right = 0; right < n; right++) {
            if (nums[right] == 0) {
                count++;
            }
            while (count > k) {
                if (nums[left] == 0) {
                    count--;
                }
                left++;
            }
            len = Math.max(len, right - left + 1);
        }
        return len;
    }
}
